//
//  HMVisitorView.swift
//  WeiBo
//
//  Created by apple on 16/6/23.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

// 定义宏的时候先可以在本类里定义,本类实现功能之后,再放到 COnstant
private let Margin = 10

// 代理的第一步
// 为什么要继承自 NSObjectProtocol 这个类?
// 因为 delegate 需要用 weak
// 协议还可以 集成自 AnyObject

protocol HMVisitorViewDelegate: NSObjectProtocol{
    
    func didLogin()
    
    func didRegister()
    
}

class HMVisitorView: UIView {

    
    // 第二步: 声明一个代理属性
   weak var delegate: HMVisitorViewDelegate?
    
   /*
        添加控件
        布局
        实现功能
    */
    
    /*
        自定义视图三部曲
    */
    
    // 第一步 -- 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步 -- 定义一个方法,来实现 视图的添加和布局
    private func setupUI(){
        
        // 设置一下视图的被背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        
        addSubview(trunImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(noticeLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 布局
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
        
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        trunImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        noticeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(trunImageView.snp_bottom).offset(Margin)
            make.width.equalTo(216)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(noticeLabel.snp_left)
            make.top.equalTo(noticeLabel.snp_bottom).offset(Margin)
            
            // 设置宽高
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            make.top.equalTo(noticeLabel.snp_bottom).offset(Margin)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
    }
    
    //MARK:-- 动画的实现
    func startAnimation(){
        
        // keyPath 不需要记住, 里边不要写错
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
    
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 10
        
        // 防止切换视图的时候,动画停止
        animation.removedOnCompletion = false
        
        trunImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    // 可选项
    //MARK:-- 对外公开一个方法,供控制器修改属性
    func setupInfo(istrun: Bool,image:String,messaeg: String){
        
        // 1.让转动的东西隐藏
        // 2.改变中间的图标
        // 3.改变提示语
        if istrun {
            
            trunImageView.hidden = false
            startAnimation()
            
        } else {
            
            trunImageView.hidden = true
        }
        
        houseImageView.image = UIImage(named: image)
        
        noticeLabel.text = messaeg
        
    }
    
    //MARK:-- 按钮的点击事件
    
    ///  登录方法
    ///
    ///  - parameter button:
    @objc private func login(button: UIButton){
        
        // 第三步: 调用
        // OC里需要判断一下 这个方法有没有实现
        // swift 里 不需要判断,因为 delegate 是一个可选的
        delegate?.didLogin()
        
    }
    ///  注册方法
    ///
    ///  - parameter button:
    @objc private func register(button: UIButton){
        
        delegate?.didRegister()
    }
    
    // 第三步: 懒加载控件
    
    private lazy var maskImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
        
    }()
    
    /// 小房子
    private lazy var houseImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    /// 转动图片
    private lazy var trunImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    /// 提示label
    private lazy var noticeLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        label.textAlignment = .Center
        
        return label
        
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "register:", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "login:", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
}
